/* * Class Name: Score * Programmer: John Carter * Date: 4/15/17 * Description: This class can store a persons name and their score. It can be * be used to keep track of a gamming score or credit score */ // You will need to change the name of this package to match your fianl project package final_start_file; public class Score implements Comparable{ //Global variables public String myName; public int myScore; //****************************************************** // Constructors //****************************************************** // If no values are passed, set both global vars to blank and 0 public Score(){ super(); myName = ""; myScore = 0; } // If only a name is passed, set the name and set the score to 0 public Score(String n){ super(); myName = n; myScore = 0; } // If both values are passed, set both public Score(String n, int s){ super(); myName = n; myScore = s; } // If both values are passed, but the score is a string, covert the score public Score(String n, String s){ super(); myName = n; myScore = Integer.parseInt(s); } //****************************************************** // SET METHODS //****************************************************** // Set just the name public void setName(String n) { myName = n; } // Set just the score public void setScore(int s) { myScore = s; } // Set just the score once it has been converted to a number public void setScore(String s) { myScore = Integer.parseInt(s); } //****************************************************** // GET METHODS //****************************************************** // Set just the name public String getName() { return myName; } // Set just the score public int getScore() { return myScore; } //****************************************************** // OUTPUT METHODS //****************************************************** // This method will display a header for the information in this class public String header(){ return " Name\tScore\n-------------------------------\n"; } // This method build a string that will display the name and score // seperated by a comma. Can be used to store the data into a file format public String store(){ return myName + "," + myScore; } // This method build a string that will display the name and score // seperated by a tab public String display(){ return " " + myName + "\t" + myScore + "\n"; } // Override the compareTo method to help the sort @Override public int compareTo(Score s){ int compareScore = ((Score) s).getScore(); // Descending order return compareScore - this.myScore; } } // End of class